// pages/send/send.js
import Toast from '../../dist/toast/toast';
import api from '../../api/api.js';
import config from '../../api/config.js';
Page({
  /**
   * 页面的初始数据
   */
  data: {
    fileList: [],
    fileLists: [],
    checked: '',
    arr: {},
    title: '',
    content: '',
    radio: '',
  },
  titles(e) {
    console.log(e.detail.value);
    this.setData({
      title: e.detail.value,
    });
  },
  onImageClick(e) {
    const id = e.currentTarget.dataset.id;
    // 更新 checked 数据，触发单选框选中
    this.setData({
      checked: id
    });
  },
  contents(e) {
    console.log(e.detail.value);
    this.setData({
      content: e.detail.value,
    });
  },
  onChange(event) {
    this.setData({
      radio: event.detail,
    });
    console.log(event.detail);
  },
  onChanges(e) {
    console.log(e);
    this.setData({
      checked: e.detail
    })
  },
  sendEvent() {
    if (this.data.title != '' && this.data.fileList != '' && this.data.fileLists != '' && this.data.checked != '' && this.data.radio != '') {
      console.log(123);
      console.log(this.data);
      //攻略
      if (this.data.radio == 1) {
        console.log(this.data);
        
        //游记
      } else if (this.data.radio == 2) {
        api.sends({
          bannar: this.data.fileLists[0].url,
            content: this.data.content,
            title: this.data.title,
            user_id: 111,
            cover: this.data.fileList[0].url
        },{
          id: 111
        }).then(response => {
          console.log( response);
          wx.showToast({
            title:'发布成功！',
            icon: 'success',
          });
          this.setData({
            fileList: [],
            fileLists: [],
            checked: '',
            title: '',
            content: '',
            radio: '',
          });
          
        }).catch(error => {
          // 登录失败后的处理
          console.error('登录失败', error);
        });
        
        //问答
      } else if (this.data.radio == 3) {
        api.sendss({
          content: this.data.content,
            user_id: 111,
            pid: 0
        },{
          id: 111
        }).then(response => {
          console.log( response);
          wx.showToast({
            title:'发布成功！',
            icon: 'success',
          });
          this.setData({
            fileList: [],
            fileLists: [],
            checked: '',
            title: '',
            content: '',
            radio: '',
          });
          
        }).catch(error => {
        });
      }
    } else {
      Toast('填写完整后才可发布');
    }
  },
  afterRead(event) {
    console.log(event);
    const {
      file
    } = event.detail;
    // 保存 this 的引用
    const that = this;
    // 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
    wx.uploadFile({
      url: config.baseUrl + '/app/File/file', // 仅为示例，非真实的接口地址
      filePath: file.url,
      name: 'file',
      formData: {
        user: 'test'
      },
      header: {
        id: 111
      },
      success(res) {
        // 上传完成需要更新 fileList
        console.log(res);
        let img_url = JSON.parse(res.data);
        console.log(img_url);
        const {
          fileList = []
        } = that.data;
        fileList.push({
          ...file,
          url: img_url.data.url
        });
        that.setData({
          fileList
        });
      },
    });

  },
  afterReads(event) {
    const {
      file
    } = event.detail;
    // 保存 this 的引用
    const that = this;
    wx.uploadFile({
      url: config.baseUrl + '/app/File/file', // 仅为示例，非真实的接口地址
      filePath: file.url,
      name: 'file',
      formData: {
        user: 'test'
      },
      header: {
        id: 111
      },
      success(res) {
        console.log(res);
        let img_url = JSON.parse(res.data);
        console.log(img_url);
        // 上传完成需要更新 fileList
        const {
          fileLists = []
        } = that.data;
        fileLists.push({
          ...file,
          url: img_url.data.url
        });
        that.setData({
          fileLists
        });
      },
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    api.getData({}).then(response => {
      // 登录成功后的处理
      console.log( response);
      this.setData({
        arr: response
      });
    }).catch(error => {
      // 登录失败后的处理
      console.error('登录失败', error);
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})